﻿using System.Text;
using UnityEngine;

namespace Game.Model
{
    /// <summary>
    /// 游戏模型管理器
    /// </summary>
    public class ModelManager
    {
        /// <summary>
        /// 全局单例
        /// </summary>
        public static ModelManager Instance { get; } = new ModelManager();

        #region GameData

        /// <summary>
        /// 角色容器
        /// </summary>
        public CharacterContainer Characters;

        #endregion
        
        /// <summary>
        /// 加载配置数据
        /// </summary>
        public void LoadData()
        {
            Data.Ability.Initialize();
            Data.Character.Initialize();
            Data.SexType.Initialize();
            Data.Skill.Initialize();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Initialize()
        {
            LoadData();
            Characters = new CharacterContainer();
            // Test only
            {
                // 先生成角色
                foreach (var characterData in Data.Character.Data)
                {
                    var amount = Random.Range(
                        characterData.AmountLimit.Min,
                        characterData.AmountLimit.Max
                        );
                    for (var i = 0; i < amount; i++)
                    {
                        Characters.NewCharacter(characterData.Id);
                    }
                }
                // 测试内容
                {
                    var characterIds = Characters.GetCharacterIds();
                    var stringBuilder = new StringBuilder();
                    for (var i = 0; i < 10; i++)
                    {
                        var character = Characters.GetCharacter(characterIds[Random.Range(0, characterIds.Length - 1)]);
                        stringBuilder.Clear();
                        stringBuilder.AppendLine($"{character.Name}: Hello! [{character.Desc}]");
                        {
                            var skills = character.GetSkillList();
                            foreach (var skillId in skills)
                            {
                                var level = character.GetSkillLevel(skillId);
                                if (level >= 0)
                                {
                                    var skillData = Data.Skill.Data[skillId];
                                    stringBuilder.AppendLine($"\t[{skillData.Name}]: Lv.{level}");
                                }
                            }
                        }
                        stringBuilder.AppendLine($"\tClass: { character.ClassLevel }");
                        stringBuilder.AppendLine($"\tMovePoint: { character.MovePoint }");
                        {
                            var target = Characters.GetCharacter(characterIds[Random.Range(0, characterIds.Length - 1)]);
                            var fakeWeaponHit = Random.Range(0.5f, 1.0f);
                            var hitRate = (character.Hit + fakeWeaponHit) - target.Avd;
                            if (Random.value < hitRate)
                            {
                                target.Hp -= Random.Range(1, 3);
                                stringBuilder.AppendLine($"\thit[{target.Name}, hp = {target.Hp}]");
                            }
                        }
                        Debug.Log(stringBuilder);
                    }
                }
            }
        }

        /// <summary>
        /// 清理
        /// </summary>
        public void Clear()
        {
            Characters = null;
        }
    }
}